package com.czh.subject.domain.service.impl;

import com.czh.subject.common.enums.CategoryTypeEnum;
import com.czh.subject.common.enums.SubjectIsDeleteEnum;
import com.czh.subject.domain.convert.SubjectLabelConverter;
import com.czh.subject.domain.entity.SubjectLabelBO;
import com.czh.subject.domain.service.SubjectLabelDomainService;
import com.czh.subject.infra.basic.entity.SubjectCategory;
import com.czh.subject.infra.basic.entity.SubjectLabel;
import com.czh.subject.infra.basic.entity.SubjectMapping;
import com.czh.subject.infra.basic.service.SubjectCategoryService;
import com.czh.subject.infra.basic.service.SubjectLabelService;
import com.czh.subject.infra.basic.service.SubjectMappingService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author czh01
 * @version 1.0
 * @description: TODO
 * @date 2024/7/30 下午 3:19
 */
@Service("subjectLabelServiceDomain")
@Log4j2
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public Boolean addSubjectLabel(SubjectLabelBO subjectLabelBO) {
        return subjectLabelService.insert(SubjectLabelConverter.INSTANCE.convert(subjectLabelBO))>0;
    }

    @Override
    public Boolean updateSubjectLabel(SubjectLabelBO subjectLabelBO) {
        return subjectLabelService.update(SubjectLabelConverter.INSTANCE.convert(subjectLabelBO))>0;
    }

    @Override
    public Boolean deleteSubjectLabel(SubjectLabelBO subjectLabelBO) {
        return subjectLabelService.deleteById(SubjectLabelConverter.INSTANCE.convert(subjectLabelBO))>0;
    }

    /**
     * TODO tip 新增标签时需要查询一级分类,其他的时候需要查询二级分类,二级分类的查询需要通过映射表
     * 新增的时候压根没有关于新增标签的题目,映射表特么当然是没有的
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果是一级分类就查出所有查出所有的标签
        SubjectCategory subjectCategory = new SubjectCategory();
        subjectCategory.setId(subjectLabelBO.getCategoryId());
        if (Objects.equals(subjectCategoryService.queryCategory(subjectCategory).get(0).getCategoryType(), CategoryTypeEnum.PRIMARY.getCode())){
            //这里映射结果中categoryId总是为null,使用Spring的Bean拷贝却是正常的,好像ma    pstruct1.6版本修复了 TODO tip
            SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convert(subjectLabelBO);
            subjectLabel.setIsDeleted(SubjectIsDeleteEnum.NO_DELETED.getCode());
            List<SubjectLabel> subjectLabelList = subjectLabelService.queryByPrimaryCategoryId(subjectLabel);
            return SubjectLabelConverter.INSTANCE.convert(subjectLabelList);
        }
        //如果是二级分类查映射表
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(subjectLabelBO.getCategoryId());
        subjectMapping.setIsDeleted(SubjectIsDeleteEnum.NO_DELETED.getCode());
        List<Long> labelIdList =  subjectMappingService.querySubjectLabelIds(subjectMapping).stream().map(SubjectMapping::getLabelId).toList();
        if (labelIdList.isEmpty()){
            return Collections.emptyList();
        }
        List<SubjectLabel>  subjectLabelList =  subjectLabelService.batchQueryByCategoryId(labelIdList);
        return SubjectLabelConverter.INSTANCE.convert(subjectLabelList);
    }
}
